package com.njupt.swg.threadstudyjimin.http;

import com.njupt.swg.threadstudyjimin.item06.threadlocal.RequestHolder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class HttpFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        log.info("do filter {},{}",Thread.currentThread().getId(),request.getServletPath());
        RequestHolder.add(1L);//userId,如果是用户信息，可以用request.getSession().getAttribute("user");获取，再add进去。
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
    }
}
